// you should have a background color on the element, or a background image
function vfade(elements, originalDirection, originalSpeed, method, arguments) {
	var timeoutSpeed = 30;

	if (window.fadeTimer) {
		window.clearTimeout(window.fadeTimer);
		window.fadeTimer = null;
	}
		
	if (!window.fadeArray) {
		window.fadeArray = new Array();
	}
	if (elements) {
		if (elements.join == undefined) {
			elements = [elements];
		}
		var directions = new Array();
		for (var i = 0; i < elements.length; i++) {
			elements[i] = (typeof(elements[i]) == 'string') ? document.getElementById(elements[i]) : elements[i];
			if (originalDirection === null || originalDirection === undefined) {
				directions[i] = null;
			}
			else if (originalDirection === true || originalDirection === false) {
				directions[i] = originalDirection;
			}
			else {
				if (originalDirection[i] === true || originalDirection[i] === false) {
					directions[i] = originalDirection[i];
				}
				else {
					directions[i] = null;
				}
			}
		}
		for (var i = 0; i < elements.length; i++) {
			var element = elements[i];
			var addItem = true;
			for (var x = 0; x < window.fadeArray.length; x++) {
				if (window.fadeArray[x].element == element) {
					window.fadeArray[x].direction = directions[i];
					if (directions[i] === null) {
						window.fadeArray[x].speed *= -1;
					}
					else if (directions[i] === true) {
						window.fadeArray[x].speed = Math.abs(window.fadeArray[x].speed);
					}
					else if (directions[i] === false) {
						window.fadeArray[x].speed = -1 * Math.abs(window.fadeArray[x].speed);
					}
					addItem = false;
					break;
				}
			}
			if (addItem) {
				if (element.currentStyle) {
					var hidden = element.currentStyle.display.toLowerCase();
					var backgroundColor = element.currentStyle.backgroundColor.toLowerCase();
				}
				else if (document.defaultView && document.defaultView.getComputedStyle) {
					var hidden = document.defaultView.getComputedStyle(element, null).getPropertyValue('display').toLowerCase();
					var backgroundColor = document.defaultView.getComputedStyle(element, null).getPropertyValue('background-color').toLowerCase();
				}
				var speed = Math.round((((100 / originalSpeed) / 1000) * timeoutSpeed) * 10) / 10;
				if (hidden != 'none') {
					if (directions[i] === true) {
						addItem = false;
					}
					hidden = false;
					speed *= -1;
				}
				else {
					if (directions[i] === false) {
						addItem = false;
					}
					hidden = true;
				}
				if (addItem) {
					if (!backgroundColor || backgroundColor == 'transparent') {
						element.style.backgroundColor = '#ffffff';
					}
					element.style.zoom = 1;
					var newClass = {
						element : element,
						speed : parseFloat(speed),
						opacity : (speed > 0) ? 0 : 100,
						direction : directions[i],
						method : method,
						arguments: arguments
					};
					window.fadeArray.push(newClass);
				}
			}
		}
	}
	
	for (var i = 0; i < window.fadeArray.length; i++) {
		var obj = window.fadeArray[i];
		obj.opacity += obj.speed;
		
		if (obj.opacity >= 100 || obj.opacity <= 0) {
			if (obj.opacity >= 100) {
				obj.element.style.opacity = 1;
				obj.element.style.filter = 'alpha(opacity=100)';
			}
			else if (obj.opacity <= 0) {
				obj.element.style.opacity = 1;
				obj.element.style.filter = 'alpha(opacity=100)';
				obj.element.style.display = 'none';
			}
			
			if (obj.method) {
				obj.method(obj.element, obj.opacity >= 100, arguments);
			}

			delete window.fadeArray[i];
			window.fadeArray[i] = null;
			window.fadeArray.splice(i, 1);
		}
		else {
			obj.element.style.opacity = obj.opacity / 100;
			obj.element.style.filter = 'alpha(opacity=' + obj.opacity + ')';
			obj.element.style.display = 'block';
		}
	}
	
	if (window.fadeArray.length > 0) {
		window.fadeTimer = window.setTimeout('vfade();', timeoutSpeed);
	}
}